package com.pzhu.acargid.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.pzhu.acargid.domain.User;
import com.pzhu.acargid.domain.UserRegister;
import com.pzhu.acargid.until.Result;
import com.pzhu.security.kaptcha.Kaptcha;
import com.pzhu.security.token.JwtTokenService;
import com.pzhu.security.token.TokenClaim;
import com.pzhu.security.token.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private Kaptcha kaptcha;

    @GetMapping("/code")
    public void Captcha(HttpSession session) {
        String render = kaptcha.render();
        session.setAttribute("code", render);
    }

    @GetMapping("/profile")
    public User profile(HttpServletRequest request) {
        Integer userid = JwtTokenService.getClaim(request, "userid").asInt();
        User byId = userService.getById(userid);
        byId.setPassword(null);
        return byId;
    }

    @PutMapping
    public boolean edit(@RequestBody User user) {
        System.out.println(user);
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userLambdaUpdateWrapper.eq(user.getId() != null, User::getId, user.getId());
        userLambdaUpdateWrapper.set(user.getName() != null, User::getName, user.getName());
        userLambdaUpdateWrapper.set(user.getEnName() != null, User::getEnName, user.getEnName());
        userLambdaUpdateWrapper.set(user.getDescription() != null, User::getDescription, user.getDescription());
        return userService.update(user, userLambdaUpdateWrapper);
    }

    @PostMapping("/login")
    public Result login(HttpSession session, @RequestBody User user) {
        String code = (String) session.getAttribute("code");
        if (code.equals(user.getCode())) {
            LambdaQueryWrapper<User> userQueryWrapper = new LambdaQueryWrapper<>();
            userQueryWrapper.eq(User::getAccount, user.getAccount());
            User byId = userService.getOne(userQueryWrapper);
            if (byId != null) {
                if (byId.getPassword().equals(user.getPassword())) {
                    String token = tokenService.generate(new TokenClaim("userid", byId.getId()));
                    return new Result(1, token, "登录成功");
                } else {
                    return new Result<>(0, null, "密码错误");
                }
            }
            return new Result<>(-1, null, "账号错误");
        } else return new Result<>(-1, null, "验证码错误");
    }

    @PostMapping("/register")
    public Result register(@RequestBody UserRegister userRegister) {
        if (userRegister.getPassword().equals(userRegister.getConfirmPassword())) {
            LambdaQueryWrapper<User> userRegisterQueryWrapper = new LambdaQueryWrapper<>();
            userRegisterQueryWrapper.eq(userRegister != null, User::getAccount, userRegister.getAccount());
            User one = userService.getOne(userRegisterQueryWrapper);
            if (one == null) {
                userRegister.setName("用户名");
                boolean save = userService.save(userRegister);
                return new Result(1, save, "注册成功");
            }
            return new Result(0, null, "账号已存在");
        }
        return new Result(-1, null, "两次密码不同");
    }
}
